package com.jerry.passenger.main.presenter;

import com.jerry.passenger.account.model.IUserManager;
import com.jerry.passenger.account.model.response.LoginResponse;
import com.jerry.passenger.common.databus.RegisterBus;
import com.jerry.passenger.common.http.biz.BaseBizResponse;
import com.jerry.passenger.common.lbs.bean.LocationInfo;
import com.jerry.passenger.common.util.LogUtil;
import com.jerry.passenger.order.bean.Order;
import com.jerry.passenger.driver.model.IDriverManager;
import com.jerry.passenger.driver.model.response.NearDriversResponse;
import com.jerry.passenger.order.model.IOrderManager;
import com.jerry.passenger.order.model.response.OrderStateOptResponse;
import com.jerry.passenger.main.view.IMainView;

public class MainPresenterImpl implements IMainPresenter {

    private IMainView view;
    private IUserManager accountManager;
    private IOrderManager orderManager;
    private IDriverManager driverManager;
    // 当前的订单
    private Order mCurrentOrder;

    public MainPresenterImpl(IMainView view,
                             IUserManager accountManager,
                             IOrderManager orderManager,
                             IDriverManager driverManager) {
        this.view = view;
        this.accountManager = accountManager;
        this.orderManager = orderManager;
        this.driverManager = driverManager;
    }

    @Override
    public void loginByToken() {
        accountManager.loginByToken();
    }

    @Override
    public boolean isLogin() {
        return accountManager.isLogin();
    }



    @Override
    public void fetchNearDrivers(double latitude, double longitude) {
        driverManager.fetchNearDrivers(latitude, longitude);
    }

    @Override
    public void updateLocationToServer(LocationInfo locationInfo) {
        driverManager.updateLocationToServer(locationInfo);
    }

    @Override
    public void callDriver(String key, float cost, LocationInfo startLocation, LocationInfo endLocation) {
        driverManager.callDriver(key, cost, startLocation, endLocation);
    }



    @Override
    public void cancel() {
        if (mCurrentOrder != null) {
            orderManager.cancel(mCurrentOrder.getOrderId());
        } else {
            view.showCancelSuc();
        }
    }

    @Override
    public void pay() {
        if (mCurrentOrder != null) {
            orderManager.pay(mCurrentOrder.getOrderId());
        }
    }

    @Override
    public void getProcessingOrder() {
        orderManager.getProcessingOrder();
    }





    @RegisterBus
    public void onLoginResponse(LoginResponse loginResponse) {
        if (loginResponse.getCode() == BaseBizResponse.STATE_OK) {
            switch (loginResponse.getCode()) {
                case IUserManager.LOGIN_SUC:
                    // 登录成功
                    view.showLoginSuc();
                    break;
                case IUserManager.TOKEN_INVALID:
                    // 登录过期
                    view.showError(IUserManager.TOKEN_INVALID, "");
                    break;
                case IUserManager.SERVER_FAIL:
                    // 服务器错误
                    view.showError(IUserManager.SERVER_FAIL, "");
                    break;
            }
        }
    }

    @RegisterBus
    public void onNearDriversResponse(NearDriversResponse nearDriversResponse){
        if (nearDriversResponse.getCode() == BaseBizResponse.STATE_OK) {
            view.showNears(nearDriversResponse.getData());
        }
    }

    @RegisterBus
    public void onLocationInfo(LocationInfo locationInfo) {
        if (mCurrentOrder != null
                &&( mCurrentOrder.getState() == OrderStateOptResponse.ORDER_STATE_ACCEPT)) {
            // 更新司机到上车点的路径信息
            view.updateDriver2StartRoute(locationInfo, mCurrentOrder);
        } else if (mCurrentOrder != null
                && mCurrentOrder.getState() == OrderStateOptResponse.ORDER_STATE_START_DRIVE){
            // 更新司机到终点的路径信息
            view.updateDriver2EndRoute(locationInfo, mCurrentOrder);
        } else {
            view.showLocationChange(locationInfo);
        }
    }

    // 订单状态响应
    @RegisterBus
    public void onOrderOptResponse(OrderStateOptResponse orderStateOptResponse) {
        if (orderStateOptResponse.getState() == OrderStateOptResponse.ORDER_STATE_CREATE) {
            // 呼叫司机
            if (orderStateOptResponse.getCode() == BaseBizResponse.STATE_OK) {
                // 保存当前的订单
                mCurrentOrder = orderStateOptResponse.getData();
                // 接口增加参数
                view.showCallDriverSuc(mCurrentOrder);
            } else {
                view.showCallDriverFail();
            }
        } else if (orderStateOptResponse.getState() == OrderStateOptResponse.ORDER_STATE_CANCEL) {
            // 取消订单
            if (orderStateOptResponse.getCode() == BaseBizResponse.STATE_OK) {
                mCurrentOrder = null;
                view.showCancelSuc();
            } else {
                view.showCancelFail();
            }
        } else if (orderStateOptResponse.getState() == OrderStateOptResponse.ORDER_STATE_ACCEPT) {
            // 司机接单
            mCurrentOrder = orderStateOptResponse.getData();
            view.showDriverAcceptOrder(mCurrentOrder);
        } else if (orderStateOptResponse.getState() == OrderStateOptResponse.ORDER_STATE_ARRIVE_START) {
            // 司机到达上车点
            mCurrentOrder = orderStateOptResponse.getData();
            view.showDriverArriveStart(mCurrentOrder);
        } else if (orderStateOptResponse.getState() == OrderStateOptResponse.ORDER_STATE_START_DRIVE) {
            // 开始行程
            mCurrentOrder = orderStateOptResponse.getData();
            view.showStartDrive(mCurrentOrder);
        } else if (orderStateOptResponse.getState() == OrderStateOptResponse.ORDER_STATE_ARRIVE_END) {
            // 到达终点
            mCurrentOrder = orderStateOptResponse.getData();
            view.showArriveEnd(mCurrentOrder);
        } else if (orderStateOptResponse.getState() == OrderStateOptResponse.PAY) {
            //  支付
            if (orderStateOptResponse.getCode() == BaseBizResponse.STATE_OK) {
                view.showPaySuc(mCurrentOrder);
            } else {
                view.showPayFail();
            }
        }
        LogUtil.d("MainPresenterImpl", "getProcessingOrder" + mCurrentOrder);
    }

}
